/**
 * Copyright (C), 2015-2021, XXX有限公司
 * FileName: MyCreditsActivity
 * Author: Gxjkz
 * Date: 2021/7/23 10:20
 * Description: 我的学分
 * History:
 * <author> <time> <version> <desc>
 * 作者姓名 修改时间 版本号 描述
 */
package com.niuxiaofa.newlaw.Activity;

import android.content.Context;
import android.util.Log;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.niuxiaofa.newlaw.Base.BaseActivity;
import com.niuxiaofa.newlaw.Bean.BaseBean;
import com.niuxiaofa.newlaw.Network.ApiService;
import com.niuxiaofa.newlaw.Network.ServiceManager;
import com.niuxiaofa.newlaw.R;
import com.niuxiaofa.newlaw.Tools.UIUtils;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import www.linwg.org.lib.LCardView;

/**
 * @ClassName: MyCreditsActivity
 * @Description: java类作用描述
 * @Author: Gxjkz
 * @Date: 2021/7/23 10:20
 */
public class MyCreditsActivity extends BaseActivity {


    @BindView(R.id.yuee)
    ImageView yuee;

    @BindView(R.id.qzb)
    ImageView qzb;

    @BindView(R.id.credit)
    EditText credit;

    @BindView(R.id.submit)
    LCardView submit;


    @BindView(R.id.xz)
    TextView xz;


    int type = 0;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_mycredits;
    }

    @Override
    protected void initData() {

        setTitle("我的学分");


        credit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {

                boolean isOK = true;
                switch (actionId) {
                    case EditorInfo.IME_ACTION_DONE:
                        InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                        manager.hideSoftInputFromWindow(credit.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

                        Call<BaseBean> call = ServiceManager.getInstance().creat(ApiService.class).creditToBalanceNeedMedalKnow(credit.getText().toString());
                        call.enqueue(new Callback<BaseBean>() {
                            @Override
                            public void onResponse(Call<BaseBean> call, Response<BaseBean> response) {
                                //判断请求是否成功
                                if (response.isSuccessful()) {
                                    BaseBean baseBean = (BaseBean) response.body();
                                    xz.setText(baseBean.getData().toString());
                                }
                            }

                            @Override
                            public void onFailure(Call<BaseBean> call, Throwable t) {
//            Log.e("链接错误", t.getMessage());
                                Toast.makeText(UIUtils.getActivity(), "网络连接异常,请稍候在试", Toast.LENGTH_LONG).show();
                            }
                        });
                        break;
                    default:
                        isOK = false;
                        break;
                }
                return isOK;
            }
        });


        yuee.setOnClickListener(e -> {
            yuee.setImageBitmap(UIUtils.getBitmap(R.mipmap.duigou));
            qzb.setImageBitmap(UIUtils.getBitmap(R.mipmap.noduigou));
        });

        qzb.setOnClickListener(e -> {
            yuee.setImageBitmap(UIUtils.getBitmap(R.mipmap.noduigou));
            qzb.setImageBitmap(UIUtils.getBitmap(R.mipmap.duigou));
        });

        submit.setOnClickListener(e -> {

            if (type == 0) {
                Call<BaseBean> call = ServiceManager.getInstance().creat(ApiService.class).creditToBalance(credit.getText().toString());
                call.enqueue(new Callback<BaseBean>() {
                    @Override
                    public void onResponse(Call<BaseBean> call, Response<BaseBean> response) {
                        //判断请求是否成功
                        if (response.isSuccessful()) {
                            BaseBean baseBean = (BaseBean) response.body();
                            Toast(baseBean.getMsg());
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseBean> call, Throwable t) {
//            Log.e("链接错误", t.getMessage());
                        Toast.makeText(UIUtils.getActivity(), "网络连接异常,请稍候在试", Toast.LENGTH_LONG).show();
                    }
                });
            } else {
                Call<BaseBean> call = ServiceManager.getInstance().creat(ApiService.class).creditToKnowCoin(credit.getText().toString());
                call.enqueue(new Callback<BaseBean>() {
                    @Override
                    public void onResponse(Call<BaseBean> call, Response<BaseBean> response) {
                        //判断请求是否成功
                        if (response.isSuccessful()) {
                            BaseBean baseBean = (BaseBean) response.body();
                            Toast(baseBean.getMsg());
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseBean> call, Throwable t) {
                        Log.e("链接错误", t.getMessage());
                        Toast.makeText(UIUtils.getActivity(), "网络连接异常,请稍候在试", Toast.LENGTH_LONG).show();
                    }
                });
            }


        });


    }
}